﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common
{
    public class Normalizer
    {
        public double[] Normalize(double[] input, double min, double max)
        {
            return input
                .Select(d => (d - min) / (max - min))
                .ToArray();
        }

        /// <summary>
        /// Normalizes the array using the list of the normalization rules.
        /// </summary>
        /// <param name="input">The input array.</param>
        /// <param name="rules">The list of the normalization rules.</param>
        /// <returns>The normalized array.</returns>
        public double[] Normalize(double[] input, List<NormalizationRule> rules)
        {
            int counter = 0;
            List<double> result = new List<double>();

            foreach (NormalizationRule rule in rules)
            {
                result.AddRange(Normalize(SubArray<double>(input, counter, rule.Quantity), rule.Minimum, rule.Maximum));
                counter += rule.Quantity;
            }

            return result.ToArray();
        }

        public double[] DeNormalize(double[] input, double min, double max)
        {
            return input
                .Select(d => (d * (max - min)) + min)
                .ToArray();
        }

        private T[] SubArray<T>(T[] data, int index, int length)
        {
            T[] result = new T[length];
            Array.Copy(data, index, result, 0, length);
            return result;
        }
    }
}
